import subprocess

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMessageBox, QLabel, QWidget, QGridLayout, QButtonGroup, QRadioButton


# TODO Requires python3-dnf-plugin-versionlock


class HPFreezeWindow(QWidget):

    def freeze_hp(self):
        dlg = QMessageBox()
        dlg.setWindowTitle('Внимание')
        hplip_update = ''
        if self.dont_refresh_hp_radiobutton.isChecked():
            subprocess.run('sudo dnf versionlock hplip*', shell=True)
            subprocess.run('sudo dnf versionlock hpip*', shell=True)
            hplip_update = 'не '
        else:
            subprocess.run('sudo dnf versionlock clear', shell=True)
            subprocess.run('sudo dnf versionlock clear hplip*', shell=True)
            subprocess.run('sudo dnf versionlock clear hpip*', shell=True)
        dlg.setText(f'Теперь пакет hplip {hplip_update}будет обновляться вместе с драйвером')
        dlg.exec()

    def check_if_hplip_blocked(self):
        response = subprocess.run('dnf versionlock list hplip*', shell=True,
                                  capture_output=True).stdout.decode().split('\n')
        while '' in response:
            response.remove('')
        return len(response) > 1

    def __init__(self):
        super().__init__()

        layout = QGridLayout()
        self.setLayout(layout)

        hplip_header_label = QLabel('Обновление плагина hplip')
        hplip_header_label.setAlignment(Qt.AlignCenter)
        layout.addWidget(hplip_header_label, 0, 0, 1, 2)
        hplip_mode_label = QLabel('Режим: ')
        hplip_mode_label.setAlignment(Qt.AlignRight)
        layout.addWidget(hplip_mode_label, 1, 0)

        refresh_hp_checkboxes = QButtonGroup()
        self.refresh_hp_radiobutton = QRadioButton('Обновлять')
        self.dont_refresh_hp_radiobutton = QRadioButton('Не обновлять')
        refresh_hp_checkboxes.addButton(self.refresh_hp_radiobutton)
        refresh_hp_checkboxes.addButton(self.dont_refresh_hp_radiobutton)
        layout.addWidget(self.refresh_hp_radiobutton, 1, 1)
        layout.addWidget(self.dont_refresh_hp_radiobutton, 2, 1)
        if self.check_if_hplip_blocked():
            self.refresh_hp_radiobutton.setChecked(True)
        else:
            self.dont_refresh_hp_radiobutton.setChecked(True)
        # self.freeze_hp_checkbox = QCheckBox('Не обновлять hplip при обновлении системы')
        # layout.addWidget(self.freeze_hp_checkbox, 0, 0, 1, 2)
        # self.freeze_hp_checkbox.setChecked(self.check_if_hplip_blocked())
        # ok_button = QPushButton('Ok')
        # cancel_button = QPushButton('Cancel')
        # layout.addWidget(ok_button, 1, 0)
        # layout.addWidget(cancel_button, 1, 1)
        # ok_button.clicked.connect(self.freeze_hp)
        # cancel_button.clicked.connect(self.close)
        # self.setWindowTitle('Заморозка пакета hplip')
        # self.show()
        layout.addWidget(QLabel(''), 3, 0)
        layout.setRowStretch(0, 0)
        layout.setRowStretch(1, 0)
        layout.setRowStretch(2, 0)
        layout.setRowStretch(3, 1)
